<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
 * this data processor provides the base for dealing with single files
 * related to a parent entry in a one-to-one fashion (i.e. one parent may
 * have one associated file).
 * 
 * data processors which inherit from this one are responsible for providing
 * a valid relational model.
 */
require_once('DataProcessor.php');

class ProcessMultipleItems extends DataProcessor{
	var $model = null;
	var $last_many_model = null;
	
	function ProcessMultipleItems( &$p ){
		parent::DataProcessor($p);
	}
	
	function _setupModel(&$params){
		if( isset($params['table']) ){
			$this->model->setTable($params['table']);
		}
		if( isset($params['relational_fields'])){
			$this->model->setRelationalFields($params['relational_fields'][0], $params['relational_fields'][1]);
		}
		if( isset($params['many_model']) ){
			$this->last_many_model = $this->model->getManyModel();
			$this->model->setManyModel($params['many_model']);
		}
	}
	
	function _teardownModel(){
		if( $this->last_many_model ){
			$this->model->setManyModel($this->last_many_model);
		}
	}
	
	/* removes relational entries, and item entries */
	function removeAllItems($id, $fields){
		if( $id ){
			$item_ids = array();
			foreach( $fields as $key => $value){
				/*
				 * grab the item ids and delete the entries from the relational table
				 */
				$this->_setupModel($value);
				$item_ids = array_merge( $item_ids, $this->model->deleteRelatedItems($id));
				$this->_teardownModel();
			}
			// delete the entries from the items table
			$this->model->deleteItems($item_ids);
		}
	}
	
	function saveNewItems($id, $fields){
		foreach( $fields as $key => $value){
			$table = $value['table'];
			if( $this->ci->db->table_exists($table)){
				$this->_setupModel($value);
				/* grab any newly uploaded items and insert them into the relational table */
				/* TODO: want to be able to process individual deletes here, e.g. if we had
				 * a checkbox to delete each new value.
				 * 
				 * need to figure out the format of the checkbox name/id so we can recreate here
				 */
				$temp = $this->ci->input->post($value['new_field']);
				if( $temp ){
					$temp = array_keys($this->_parseOptionString($temp));
					$this->model->addRelatedItems( $id, $temp);
					unset($temp);
				}
				
				/* update the selected items in the related database */
				/*
				 * probably want to add an auto-select feature for things like multiple
				 * documents where all options are always selected
				 */
				$this->model->updateSelectedRelatedItems( $id, $this->ci->input->post($key));
				
				if( isset($value['sort_field'])){
					if( isset($value['table_order_field']) ){
						$this->model->setOrderField($value['table_order_field']);
					}
					$temp = $this->ci->input->post($value['sort_field']);
					if( $temp ){
						parse_str($temp);
						$this->model->saveSelectedRelatedItemsOrder( $id, ${$value['sort_field']});
					}
				}
				$this->_teardownModel();
			}else{
				show_error("ProcessMultipleItems::saveItem - table $key does not exist.");
			}
		}
	}
	
	function removeNewItems($id, $fields){
		$item_ids = array();
		foreach( $fields as $key => $value){
			$temp = $this->ci->input->post($value['new_field']);
			if( $temp ){
				$temp = array_keys($this->_parseOptionString($temp));
				$item_ids = array_merge( $item_ids, $temp);
			}
		}
		$this->model->deleteItems($item_ids);
	}
	
	function addItemOptions($id, $fields){
		$params = array();
		foreach( $fields as $key => $value){
			$table = $value['table'];
			if( $this->ci->db->table_exists($table)){
				$options = array();
				$selected = array();
				
				$this->_setupModel($value);
				/* if this is an edit add the database entries to the options array */
				if( !empty($id) ){
					if( isset($value['table_order_field'])){
						$this->model->setOrderField($value['table_order_field']);
					}
					$options = $options + $this->model->getItemOptions($id);
					if( !$this->ci->input->post($key) || !is_array($this->ci->input->post($key))){
						$selected = $selected + $this->model->getSelectedItems($id);
					}
					/*echo "getting options and selected from database: $id";
					echo_html_object($options);
					echo_html_object($selected);*/
				}
				
				/* check if the user has uploaded any new items and add them to the options array
				 * also add a hidden field to keep track of item ids that must be added to the relational database
				 * after save or deleted from the items database on cancel
				 */
				$new_field = $value['new_field'];
				if( $this->ci->input->post($new_field)){
					$options = $options + $this->_parseOptionString($this->ci->input->post($new_field));
					
					/* this should be a function call*/
					$this->parent_model->fields[$new_field] = array(
													'edit'	=> array(
																'func'		=> array('Form_wrapper', 'form_input_wrapper'),
																'params'	=> array( array('value' => $this->ci->input->post($new_field),'id' => $new_field, 'type' => 'hidden')),
																'label'		=> false,
																'hidden'	=> true
															)
												);
					//echo "getting options from new field: $new_field";
					//echo_html_object($options);
				}
				
				/* if this is a re-edit and an altered selection exists use that instead */
				if( $this->ci->input->post($key) && is_array($this->ci->input->post($key))){
					unset($selected);
					$selected = $this->ci->input->post($key);
					//echo "getting selected from post($key)";
					//echo_html_object($selected);
				}
				
				$params[$key] = array($options, $selected);
				unset($options);
				unset($selected);
				$this->_teardownModel();
			}else{
				show_error("ProcessMultipleItems::addItemOptionParameters - table $key does not exist");
			}
		}
		//echo "params:";
		//echo_html_object($params);
		$this->parent_model->addEditParameters($params);
	}
	
}

?>